function result = nangeomean(X, dim)

% computes geometric mean ignoring NaNs
% dim = 1 is mean of columns and dim = 2 is mean of rows 
% default is to take mean of columns
% REMEMBER: cannot take geometric mean of negative numbers. Returns must in
% GROSS form 

if isempty(dim) == 1
    dim = 1;
end


[T , N] = size(X);

if dim == 1
    
    result = NaN(1,N);
    
    for n = 1 : N
        temp = X(:,n);
        s = isnan(temp);
        temp = temp(~s);
        result(1,n) = geomean(temp);
    end
    
elseif dim == 2
    
    result = NaN(T,1);
    
    for t = 1 : T
        temp = X(t,:);
        s = isnan(temp);
        temp = temp(~s);
        result(t,1) = geomean(temp);
    end
    
end
